package com.css.fxfzypg.modules.saresults.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.repository.AttachmentInfoRepository;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.saresults.entity.*;
import com.css.fxfzypg.modules.saresults.param.AddApParam;
import com.css.fxfzypg.modules.saresults.param.AddQpParam;
import com.css.fxfzypg.modules.saresults.param.ApTemporarySaveParam;
import com.css.fxfzypg.modules.saresults.repository.QpResultsRepository;
import com.css.fxfzypg.modules.saresults.repository.SaResultsRepository;
import com.css.fxfzypg.modules.saresults.service.QpResultsService;
import com.css.fxfzypg.modules.saresults.util.ExcelUtil;
import com.css.fxfzypg.modules.saresults.util.RecordUtils;
import com.css.fxfzypg.modules.saresults.vo.SaResultsHlVo;
import com.css.fxfzypg.modules.saresults.vo.SaResultsIdVo;
import com.css.fxfzypg.modules.saresults.vo.SaResultsVo;
import com.css.fxfzypg.util.*;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.css.fxfzypg.zcpt.sys.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Slf4j
public class QpResultsServiceImpl implements QpResultsService {

    @Autowired
    private SaResultsRepository saResultsRepository;

    @Autowired
    private QpResultsRepository qpResultsRepository;

    @Autowired
    private SUserService sUserService;

    @Autowired
    private SaResultsServiceImpl saResultsService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysAreaService sysAreaService;

    @Resource
    private AttachmentInfoRepository attachmentInfoRepository;

    @Autowired
    private RecordUtils recordUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveOrUpdateQpResults(SaResultsVo saResultsVo) {
        String newProjectId = UUIDGenerator.getUUID();

        DzqhQpBasicMapEntity qpAsicMapEntity = saResultsVo.getDzqhQpBasicMapEntity();
        List<DzqhApCruxDhEntity> dzqhApCruxDhEntityList = saResultsVo.getDzqhApCruxDhEntityList();
        List<DzqhApCruxElEntity> dzqhApCruxElEntityList = saResultsVo.getDzqhApCruxElEntityList();
        List<DzqhApCruxHecEntity> dzqhApCruxHecEntityList = saResultsVo.getDzqhApCruxHecEntityList();
        List<DzqhApCruxLayerPfEntity> dzqhApCruxLayerPfEntityList = saResultsVo.getDzqhApCruxLayerPfEntityList();
        List<DzqhApCruxScEntity> dzqhApCruxScEntityList = saResultsVo.getDzqhApCruxScEntityList();
        List<DzqhApCruxSdEntity> dzqhApCruxSdEntityList = saResultsVo.getDzqhApCruxSdEntityList();
        List<DzqhApCruxSgmEntity> dzqhApCruxSgmEntityList = saResultsVo.getDzqhApCruxSgmEntityList();
        try {
            //查询当前登陆人角色
            String userId = PlatformSessionContext.getUserID();
            String role = roleService.getRoleByUserId(userId);
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            ArrayList<String> roleList = new ArrayList<>();
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                roleList.add(roleCode);
            }

            String user = sUserService.getSUser(PlatformSessionContext.getUserID());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            List<DzqhQpBasicMapEntity> dzqhQpBasicMapEntityList = qpResultsRepository.queryProjectByProjectName(qpAsicMapEntity.getProjectName());

            if (!PlatformObjectUtils.isEmpty(qpAsicMapEntity.getProjectId())) {
                //查出添加人和添加时间
                DzqhQpBasicMapEntity entity = qpResultsRepository.queryDzqhQpBasicMap(qpAsicMapEntity.getProjectId());
                String createUser = entity.getCreateUser();
                Date createTime = entity.getCreateTime();

                if (!roleList.contains(FxfzConstants.DZQH_GL_ROLE) && !roleList.contains(FxfzConstants.DZQH_GAME_GL_ROLE) && !roleList.contains(FxfzConstants.DZQH_PRI_GL_ROLE) && !userId.equals(createUser)) {
                    return RestResponse.fail("只能修改本人数据");
                } else if (entity.getSubStatus().equals("0") && qpAsicMapEntity.getSubStatus().equals("1")) {
                    //保存状态下的提交
                    if (dzqhQpBasicMapEntityList.size() != 0) {
                        return RestResponse.fail("项目名称已存在");
                    }
                    qpAsicMapEntity.setProjectId(newProjectId);
                    qpAsicMapEntity.setCreateUser(PlatformSessionContext.getUserID());
                    qpAsicMapEntity.setOriginalDataId(qpAsicMapEntity.getProjectId());
                    qpAsicMapEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                    qpAsicMapEntity.setIsDelete(YNEnum.N.toString());
                    qpAsicMapEntity.setStatus("0");
                    qpAsicMapEntity.setApprovalStatus("0");
                    qpAsicMapEntity.setUpdateStatus("0");
                    qpAsicMapEntity.setProvinceId(sUser.getProvinceId());
                    qpAsicMapEntity.setOrgId(sUser.getOrgId());
                    qpAsicMapEntity.setModel(FxfzConstants.DZQH_QP_MODEL);
                    qpAsicMapEntity.setRegistrationUnit(sUser.getOrgName());
                    qpAsicMapEntity.setRegistrant(sUser.getUserName());
                    qpAsicMapEntity.setSubStatus(qpAsicMapEntity.getSubStatus());
                    qpResultsRepository.saveDzqhQpBasicMap(qpAsicMapEntity);
                    qpResultsRepository.deleteQpAsicMap(entity.getProjectId());
//                        qpResultsRepository.updateStatus(qpAsicMapEntity.getProjectId());
                } else if (!PlatformObjectUtils.isEmpty(qpAsicMapEntity.getUpdateReason()) && entity.getApprovalStatus().equals("0") && entity.getStatus().equals("0")) {
                    if (roleList.contains(FxfzConstants.DZQH_GL_ROLE) || roleList.contains(FxfzConstants.DZQH_GAME_GL_ROLE) || roleList.contains(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                        //管理员修改普通用户数据
                        qpResultsRepository.updateReason(qpAsicMapEntity.getUpdateReason(), qpAsicMapEntity.getProjectId());
                    }
                } else if (roleList.contains(FxfzConstants.DZQH_GL_ROLE) || roleList.contains(FxfzConstants.DZQH_GAME_GL_ROLE) || roleList.contains(FxfzConstants.DZQH_PRI_GL_ROLE) && !qpAsicMapEntity.getSubStatus().equals("0") && !PlatformObjectUtils.isEmpty(qpAsicMapEntity.getReviewComments())) {
                    //管理员审批
                    if (qpAsicMapEntity.getApprovalStatus().equals("2")) {
                        //审批通过
                        String status = "2";
                        qpResultsRepository.updateApprovalStatus(status, qpAsicMapEntity.getOriginalDataId(), qpAsicMapEntity.getReviewComments(), qpAsicMapEntity.getApprovalStatus(), sUser.getUserName(), sUser.getOrgName(), PlatformDateUtils.getCurrentTimestamp());
                        //删除修改之前的数据
                        qpResultsRepository.deleteQpAsicMap(qpAsicMapEntity.getProjectId());
                        //记录操作记录
                        String journalStatus = "1";
                        saResultsRepository.updateJournalStatus(qpAsicMapEntity.getProjectId(), journalStatus);
                    } else if (qpAsicMapEntity.getApprovalStatus().equals("3")) {
                        //审批未通过
                        String status = "1";
                        qpResultsRepository.auditFailed(status, qpAsicMapEntity.getProjectId(), qpAsicMapEntity.getReviewComments(), qpAsicMapEntity.getApprovalStatus(), sUser.getUserName(), sUser.getOrgName(), PlatformDateUtils.getCurrentTimestamp());
                        //记录操作记录
                        String journalStatus = "2";
                        saResultsRepository.updateJournalStatus(qpAsicMapEntity.getProjectId(), journalStatus);
                    }
                }
//                if (!PlatformObjectUtils.isEmpty(qpAsicMapEntity.getProjectId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
//                    //保存状态下的修改
//                    qpAsicMapEntity.setLongitudeString(StringUtils.strip(Arrays.toString(qpAsicMapEntity.getLongitude()), "[]"));
//                    qpAsicMapEntity.setLatitudeString(StringUtils.strip(Arrays.toString(qpAsicMapEntity.getLatitude()), "[]"));
//                    qpResultsRepository.updateDzqhQpBasicMap(qpAsicMapEntity);
//                }
                if (entity.getCreateUser().equals(userId) && !entity.getStatus().equals("3") && !entity.getStatus().equals("4") && !entity.getApprovalStatus().equals("1")) {
                    //更改旧数据状态
                    qpResultsRepository.updateOldStatus(entity.getProjectId(), qpAsicMapEntity.getUpdateReason());
                    qpAsicMapEntity.setOriginalDataId(entity.getOriginalDataId());
                    qpAsicMapEntity.setProjectId(newProjectId);
                    qpAsicMapEntity.setCreateUser(createUser);
                    qpAsicMapEntity.setUpdateReason(entity.getUpdateReason());
                    qpAsicMapEntity.setRegistrant(sUser.getUserName());
                    qpAsicMapEntity.setCreateTime(createTime);
                    qpAsicMapEntity.setUpdateUser(PlatformSessionContext.getUserID());
                    qpAsicMapEntity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                    qpAsicMapEntity.setIsDelete(YNEnum.N.toString());
                    qpAsicMapEntity.setModel(FxfzConstants.DZQH_QP_MODEL);
                    qpAsicMapEntity.setStatus("2");
                    qpAsicMapEntity.setProvinceId(sUser.getProvinceId());
                    qpAsicMapEntity.setOrgId(sUser.getOrgId());
                    qpAsicMapEntity.setSubStatus(qpAsicMapEntity.getSubStatus());
                    qpAsicMapEntity.setUpdateStatus("1");
                    //设置状态为待审批
                    qpAsicMapEntity.setApprovalStatus("2");
                    qpResultsRepository.saveDzqhQpBasicMap(qpAsicMapEntity);
                    //记录基本数据表操作记录
                    DzqhJournalEntity journal = new DzqhJournalEntity();
                    journal.setStatus("1");
                    saveQpAsicMapOperation(qpAsicMapEntity, journal, sUser);
                }
            } else {
                //根据项目名字查询项目
                if (dzqhQpBasicMapEntityList.size() != 0) {
                    return RestResponse.fail("项目名称已存在");
                }
                //项目id为空，走添加
                qpAsicMapEntity.setProjectId(newProjectId);
                qpAsicMapEntity.setCreateUser(PlatformSessionContext.getUserID());
                qpAsicMapEntity.setOriginalDataId(qpAsicMapEntity.getProjectId());
                qpAsicMapEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                qpAsicMapEntity.setIsDelete(YNEnum.N.toString());
                qpAsicMapEntity.setStatus("0");
                qpAsicMapEntity.setApprovalStatus("0");
                qpAsicMapEntity.setUpdateStatus("0");
                qpAsicMapEntity.setProvinceId(sUser.getProvinceId());
                qpAsicMapEntity.setOrgId(sUser.getOrgId());
                qpAsicMapEntity.setModel(FxfzConstants.DZQH_QP_MODEL);
                qpAsicMapEntity.setRegistrationUnit(sUser.getOrgName());
                qpAsicMapEntity.setRegistrant(sUser.getUserName());
                qpAsicMapEntity.setSubStatus(qpAsicMapEntity.getSubStatus());
                qpResultsRepository.saveDzqhQpBasicMap(qpAsicMapEntity);
            }

            if (dzqhApCruxDhEntityList != null)
                dzqhApCruxDhEntityList.stream().forEach(it -> {
                    if (!PlatformObjectUtils.isEmpty(it.getGeotechnicalId())) {
                        it.setGeotechnicalId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCruxDh(it);
                        saResultsService.saveApCruxDhOperation(it, sUser);
                    } else if (!PlatformObjectUtils.isEmpty(it.getGeotechnicalId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                        saResultsRepository.updateApCruxDh(it);
                    } else {
                        it.setGeotechnicalId(UUIDGenerator.getUUID());
                        it.setProjectId(qpAsicMapEntity.getProjectId());
                        saResultsRepository.saveApCruxDh(it);
                    }
                });

            if (dzqhApCruxElEntityList != null)
                dzqhApCruxElEntityList.stream().forEach(it -> {
                    if (!PlatformObjectUtils.isEmpty(it.getTestId())) {
                        it.setTestId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCzruxEl(it);
                        saResultsService.saveCruxElOperation(it, sUser);
                    } else if (!PlatformObjectUtils.isEmpty(it.getTestId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                        saResultsRepository.updateApCzruxEl(it);
                    } else {
                        it.setTestId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCzruxEl(it);
                    }
                });

            if (dzqhApCruxHecEntityList != null)
                dzqhApCruxHecEntityList.stream().forEach(it -> {
                    if (!PlatformObjectUtils.isEmpty(it.getEcId())) {
                        it.setEcId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCruxHec(it);
                        saResultsService.saveCruxHecOperation(it, sUser);
                    } else if (!PlatformObjectUtils.isEmpty(it.getEcId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                        saResultsRepository.updateApCruxHec(it);
                    } else {
                        it.setEcId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCruxHec(it);
                    }
                });

            if (dzqhApCruxLayerPfEntityList != null)
                dzqhApCruxLayerPfEntityList.stream().forEach(it -> {
                    if (!PlatformObjectUtils.isEmpty(it.getPfId())) {
                        it.setPfId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        it.setCreateUser(qpAsicMapEntity.getCreateUser());
                        it.setCreateTime(qpAsicMapEntity.getCreateTime());
                        it.setIsDelete(YNEnum.N.toString());
                        it.setDataType(FxfzConstants.DZQH_QP_MODEL);
                        saResultsRepository.saveApCruxLayerPf(it);
                        saResultsService.saveCruxLayerPfOperation(it, sUser);
                    } else if (!PlatformObjectUtils.isEmpty(it.getPfId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                        saResultsRepository.updateApCruxLayerPf(it);
                    } else {
                        it.setPfId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        it.setCreateUser(qpAsicMapEntity.getCreateUser());
                        it.setCreateTime(qpAsicMapEntity.getCreateTime());
                        it.setIsDelete(YNEnum.N.toString());
                        it.setDataType(FxfzConstants.DZQH_QP_MODEL);
                        saResultsRepository.saveApCruxLayerPf(it);
                    }

                    if (dzqhApCruxScEntityList != null)
                        dzqhApCruxScEntityList.stream().forEach(sc -> {
                            if (sc.getBoreholeNumber().equals(it.getBoreholeNumber())) {
                                if (!PlatformObjectUtils.isEmpty(sc.getScDistinguishId())) {
                                    sc.setScDistinguishId(UUIDGenerator.getUUID());
                                    sc.setProjectId(newProjectId);
                                    saResultsRepository.saveApCruxSc(sc);
                                    saResultsService.saveCruxScOperation(sc, sUser);
                                } else if (!PlatformObjectUtils.isEmpty(sc.getScDistinguishId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                                    saResultsRepository.updateApCruxSc(sc);
                                } else {
                                    sc.setScDistinguishId(UUIDGenerator.getUUID());
                                    sc.setProjectId(newProjectId);
                                    saResultsRepository.saveApCruxSc(sc);
                                }
                            }
                        });
                });

//            if (dzqhApCruxScEntityList != null)
//                dzqhApCruxScEntityList.stream().forEach(it -> {
//                    if (!PlatformObjectUtils.isEmpty(it.getScDistinguishId())) {
//                        it.setScDistinguishId(UUIDGenerator.getUUID());
//                        it.setProjectId(newProjectId);
//                        saResultsRepository.saveApCruxSc(it);
//                        saResultsService.saveCruxScOperation(it, sUser);
//                    } else if (!PlatformObjectUtils.isEmpty(it.getScDistinguishId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
//                        saResultsRepository.updateApCruxSc(it);
//                    } else {
//                        it.setScDistinguishId(UUIDGenerator.getUUID());
//                        it.setProjectId(newProjectId);
//                        saResultsRepository.saveApCruxSc(it);
//                    }
//                });

            if (dzqhApCruxSdEntityList != null)
                dzqhApCruxSdEntityList.stream().forEach(it -> {
                    if (!PlatformObjectUtils.isEmpty(it.getSdId())) {
                        it.setSdId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCruxSd(it);
                        saResultsService.saveCruxSdOperation(it, sUser);
                    } else if (!PlatformObjectUtils.isEmpty(it.getSdId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                        saResultsRepository.updateApCruxSd(it);
                    } else {
                        it.setSdId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCruxSd(it);
                    }
                });

            if (dzqhApCruxSgmEntityList != null)
                dzqhApCruxSgmEntityList.stream().forEach(it -> {
                    if (!PlatformObjectUtils.isEmpty(it.getSgmId())) {
                        it.setSgmId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCruxSgm(it);
                        saResultsService.saveCruxSgmOperation(it, sUser);
                    } else if (!PlatformObjectUtils.isEmpty(it.getSgmId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                        saResultsRepository.updateApCruxSgm(it);
                    } else {
                        it.setSgmId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveApCruxSgm(it);
                    }
                });

            List<DzqhProfileInformationEntity> profileInformationList = saResultsVo.getProfileInformationList();
            if (profileInformationList != null)
                profileInformationList.stream().forEach(it -> {
                    if (!PlatformObjectUtils.isEmpty(it.getBisectId())) {
                        it.setBisectId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveProfileInfo(it);
                        saResultsService.saveProfileInfoOperation(it, sUser);
                    } else if (!PlatformObjectUtils.isEmpty(it.getBisectId()) && qpAsicMapEntity.getSubStatus().equals("0")) {
                        saResultsRepository.updateProfileInfo(it);
                    } else {
                        it.setBisectId(UUIDGenerator.getUUID());
                        it.setProjectId(newProjectId);
                        saResultsRepository.saveProfileInfo(it);
                    }
                });
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
        return RestResponse.succeed("成功!");
    }

    private void saveQpAsicMapOperation(DzqhQpBasicMapEntity de, DzqhJournalEntity journal, SUser sUser) {
        journal.setProjectId(de.getOriginalDataId());
        journal.setOperation("修改");
        journal.setCompany(sUser.getOrgName());
        journal.setOperationUser(de.getUpdateUser());
        journal.setUserName(sUser.getUserName());
        journal.setOperationTime(de.getUpdateTime());
        journal.setStatus("0");
        journal.setTableName("项目基本情况表");
        //查询出修改之前的数据
        DzqhQpBasicMapEntity asicMap = qpResultsRepository.queryDzqhQpBasicMap(de.getOriginalDataId());
        if (!PlatformObjectUtils.isEmpty(de.getProjectName()) && !de.getProjectName().equals(asicMap.getProjectName())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("项目名称");
            journal.setBeforeUpdating(asicMap.getProjectName());
            journal.setAfterUpdate(de.getProjectName());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSurvey()) && !de.getSurvey().equals(asicMap.getSurvey())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("项目概况");
            journal.setBeforeUpdating(asicMap.getSurvey());
            journal.setAfterUpdate(de.getSurvey());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getCpFile()) && !de.getCpFile().equals(asicMap.getCpFile())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("上传文件");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getAdministrativeRegion()) && !de.getAdministrativeRegion().equals(asicMap.getAdministrativeRegion())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("所在行政区");
            journal.setBeforeUpdating(asicMap.getAdministrativeRegion());
            journal.setAfterUpdate(de.getAdministrativeRegion());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSubDate()) && !de.getSubDate().equals(asicMap.getSubDate())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("报告提交日期");
            journal.setBeforeUpdating(asicMap.getSubDate().toString());
            journal.setAfterUpdate(de.getSubDate().toString());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getReviewUnit()) && !de.getReviewUnit().equals(asicMap.getReviewUnit())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("评审单位");
            journal.setBeforeUpdating(asicMap.getReviewUnit());
            journal.setAfterUpdate(de.getReviewUnit());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getUndertaker()) && !de.getUndertaker().equals(asicMap.getUndertaker())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("承担单位");
            journal.setBeforeUpdating(asicMap.getUndertaker());
            journal.setAfterUpdate(de.getUndertaker());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getClient()) && !de.getClient().equals(asicMap.getClient())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("委托单位");
            journal.setBeforeUpdating(asicMap.getClient());
            journal.setAfterUpdate(de.getClient());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getCpNumber()) && !de.getCpNumber().equals(asicMap.getCpNumber())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("控制点个数");
            journal.setBeforeUpdating(asicMap.getCpNumber().toString());
            journal.setAfterUpdate(de.getCpNumber().toString());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getGeophysicalSurveyLine()) && !de.getGeophysicalSurveyLine().equals(asicMap.getGeophysicalSurveyLine())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("物探测线");
            journal.setBeforeUpdating(asicMap.getGeophysicalSurveyLine().toString());
            journal.setAfterUpdate(de.getGeophysicalSurveyLine().toString());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getEvaluationResults()) && !de.getEvaluationResults().equals(asicMap.getEvaluationResults())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("评价结果");
            journal.setBeforeUpdating(asicMap.getEvaluationResults());
            journal.setAfterUpdate(de.getEvaluationResults());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getRegistrationUnit()) && !de.getRegistrationUnit().equals(asicMap.getRegistrationUnit())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("登记单位");
            journal.setBeforeUpdating(asicMap.getRegistrationUnit());
            journal.setAfterUpdate(de.getRegistrationUnit());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getRsMap()) && !de.getRsMap().equals(asicMap.getRsMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("区域地震构造图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getNewFpMap()) && !de.getNewFpMap().equals(asicMap.getNewFpMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("本次新增加的断层剖面图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getNfssMap()) && !de.getNfssMap().equals(asicMap.getNfssMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("近场地震分布图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getReedMap()) && !de.getReedMap().equals(asicMap.getReedMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("区域地震震中分布图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getNfeedMap()) && !de.getNfeedMap().equals(asicMap.getNfeedMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("近场地震震中分布图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getPsaMap()) && !de.getPsaMap().equals(asicMap.getPsaMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("潜在震源区图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getBhMap()) && !de.getBhMap().equals(asicMap.getBhMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("钻孔柱状图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSepz63()) && !de.getSepz63().equals(asicMap.getSepz63())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("地震动参数分区50年63%");
            journal.setBeforeUpdating(asicMap.getSepz63());
            journal.setAfterUpdate(de.getSepz63());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSepz10()) && !de.getSepz10().equals(asicMap.getSepz10())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("地震动参数分区50年10%");
            journal.setBeforeUpdating(asicMap.getSepz10());
            journal.setAfterUpdate(de.getSepz10());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSepz2()) && !de.getSepz10().equals(asicMap.getSepz2())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("地震动参数分区50年2%");
            journal.setBeforeUpdating(asicMap.getSepz2());
            journal.setAfterUpdate(de.getSepz2());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSepz()) && !de.getSepz().equals(asicMap.getSepz())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("地震动参数分区");
            journal.setBeforeUpdating(asicMap.getSepz());
            journal.setAfterUpdate(de.getSepz());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSaghdMap()) && !de.getSaghdMap().equals(asicMap.getSaghdMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("地震地质灾害分布图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getBdMap()) && !de.getBdMap().equals(asicMap.getBdMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("钻孔分布图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getEgpMap()) && !de.getEgpMap().equals(asicMap.getEgpMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("工程地质剖面图");
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getNewTrenching()) && !de.getNewTrenching().equals(asicMap.getNewTrenching())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("本次新增加的探槽");
            journal.setBeforeUpdating(asicMap.getNewTrenching());
            journal.setAfterUpdate(de.getNewTrenching());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getRowDrillMap()) && !de.getRowDrillMap().equals(asicMap.getRowDrillMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("排钻图片");
            journal.setBeforeUpdating(asicMap.getRowDrillMap());
            journal.setAfterUpdate(de.getRowDrillMap());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getLsafMap()) && !de.getLsafMap().equals(asicMap.getLsafMap())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("大比例尺活动断层分布图");
            journal.setBeforeUpdating(asicMap.getLsafMap());
            journal.setAfterUpdate(de.getLsafMap());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getSaghEvaluation()) && !de.getSaghEvaluation().equals(asicMap.getSaghEvaluation())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("地震地质灾害评价");
            journal.setBeforeUpdating(asicMap.getSaghEvaluation());
            journal.setAfterUpdate(de.getSaghEvaluation());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getPeriodicPoint()) && !de.getPeriodicPoint().equals(asicMap.getPeriodicPoint())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("周期点");
            journal.setBeforeUpdating(asicMap.getPeriodicPoint());
            journal.setAfterUpdate(de.getPeriodicPoint());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getRsValue()) && !de.getRsValue().equals(asicMap.getRsValue())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("反应谱值");
            journal.setBeforeUpdating(asicMap.getRsValue());
            journal.setAfterUpdate(de.getRsValue());
            saResultsRepository.saveJournal(journal);
        }
        if (!PlatformObjectUtils.isEmpty(de.getUploadReport()) && !de.getUploadReport().equals(asicMap.getUploadReport())) {
            journal.setJournalId(UUIDGenerator.getUUID());
            journal.setField("上传报告");
            journal.setBeforeUpdating(asicMap.getUploadReport());
            journal.setAfterUpdate(de.getUploadReport());
            saResultsRepository.saveJournal(journal);
        }

//        if (!PlatformObjectUtils.isEmpty(de.getLongitude()) && !Arrays.toString(de.getLongitude()).equals(Arrays.toString(asicMap.getLongitude()))) {
//            journal.setJournalId(UUIDGenerator.getUUID());
//            journal.setField("经度");
//            journal.setBeforeUpdating(Arrays.toString(asicMap.getLongitude()));
//            journal.setAfterUpdate(Arrays.toString(de.getLongitude()));
//            saResultsRepository.saveJournal(journal);
//        }
//        if (!PlatformObjectUtils.isEmpty(de.getLatitude()) && !Arrays.toString(de.getLatitude()).equals(Arrays.toString(asicMap.getLatitude()))) {
//            journal.setJournalId(UUIDGenerator.getUUID());
//            journal.setField("纬度");
//            journal.setBeforeUpdating(Arrays.toString(asicMap.getLatitude()));
//            journal.setAfterUpdate(Arrays.toString(de.getLatitude()));
//            saResultsRepository.saveJournal(journal);
//        }
    }

    @Override
    public RestResponse getSaResultsById(String projectId) {
        SaResultsVo saResultsVo = new SaResultsVo();
        HashMap<String, Object> map = new HashMap<>();

        try {
            DzqhQpBasicMapEntity qpBasicMapEntity = qpResultsRepository.queryDzqhQpBasicMap(projectId);
            queryFile(map, qpBasicMapEntity.getProjectId());
            saResultsVo.setDzqhQpBasicMapEntity(qpBasicMapEntity);
            saResultsVo.setDzqhApCruxDhEntityList(saResultsRepository.queryDzqhApCruxDh(projectId));
            List<DzqhApCruxElEntity> elList = saResultsRepository.queryDzqhApCruxEl(projectId);
            elList.forEach(it -> {
                if (PlatformObjectUtils.isNotEmpty(it.getPposlMap())) {
                    AttachmentInfoEntity page = attachmentInfoRepository.getAttach(it.getPposlMap());
                    it.setPposlMapPage(page);
                }
            });
            saResultsVo.setDzqhApCruxElEntityList(elList);
            saResultsVo.setDzqhApCruxHecEntityList(saResultsRepository.queryDzqhApCruxHec(projectId));
            saResultsVo.setDzqhApCruxLayerPfEntityList(saResultsRepository.queryDzqhApCruxLayerPf(projectId));
            saResultsVo.setDzqhApCruxScEntityList(saResultsRepository.queryDzqhApCruxSc(projectId));
            saResultsVo.setDzqhApCruxSdEntityList(saResultsRepository.queryDzqhApCruxSd(projectId));
            saResultsVo.setDzqhApCruxSgmEntityList(saResultsRepository.queryDzqhApCruxSgm(projectId));
            List<DzqhProfileInformationEntity> infoList = saResultsRepository.queryprofileInfo(projectId);
            infoList.forEach(it -> {
                if (PlatformObjectUtils.isNotEmpty(it.getPposlMap())) {
                    AttachmentInfoEntity page = attachmentInfoRepository.getAttach(it.getPposlMap());
                    it.setPposlMapPage(page);
                }
            });
            saResultsVo.setProfileInformationList(infoList);
            saResultsVo.setFileMap(map);

            String userId = PlatformSessionContext.getUserID();
            String role = roleService.getRoleByUserId(userId);
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);

            ArrayList<String> roleList = new ArrayList<>();
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                roleList.add(roleCode);
            }

            if (roleList.contains(FxfzConstants.DZQH_GL_ROLE)) {
                String status = "0";
                List<DzqhJournalEntity> dzqhJournalEntities = saResultsRepository.queryHistory(qpBasicMapEntity.getOriginalDataId(), status);
                saResultsVo.setDzqhJournalEntityList(dzqhJournalEntities);
            }
        } catch (Exception e) {
            String errorMessage = "区评成果目录查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
        return RestResponse.succeed(saResultsVo);
    }

    public void queryFile(HashMap<String, Object> map, String projectId) {
        DzqhQpBasicMapEntity qpBasicMapEntity = qpResultsRepository.queryDzqhQpBasicMap(projectId);
        map.put("rsMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getRsMap()));
        map.put("newFpMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getNewFpMap()));
        map.put("nfSsMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getNfssMap()));
        map.put("reedMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getReedMap()));
        map.put("nfeedMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getNfeedMap()));
        map.put("psaMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getPsaMap()));
        map.put("bhMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getBhMap()));
        map.put("sepz63", attachmentInfoRepository.getAttach(qpBasicMapEntity.getSepz63()));
        map.put("sepz10", attachmentInfoRepository.getAttach(qpBasicMapEntity.getSepz10()));
        map.put("sepz2", attachmentInfoRepository.getAttach(qpBasicMapEntity.getSepz2()));
        String sepz = qpBasicMapEntity.getSepz();
        if (PlatformObjectUtils.isNotEmpty(sepz)) {
            String[] split = sepz.split(",");
            ArrayList<AttachmentInfoEntity> attachmentInfoEntities = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                attachmentInfoEntities.add(attachmentInfoRepository.getAttach(split[i]));
            }
            map.put("sepz", attachmentInfoEntities);
        }
        map.put("saghdMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getSaghdMap()));
        map.put("bdMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getBdMap()));
        map.put("egpMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getEgpMap()));
        map.put("newTrenching", attachmentInfoRepository.getAttach(qpBasicMapEntity.getNewTrenching()));
        map.put("rowDrillMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getRowDrillMap()));
        map.put("lsafMap", attachmentInfoRepository.getAttach(qpBasicMapEntity.getLsafMap()));
        map.put("uploadReport", attachmentInfoRepository.getAttach(qpBasicMapEntity.getUploadReport()));
        map.put("geologicalProfileImage", attachmentInfoRepository.getAttach(qpBasicMapEntity.getGeologicalProfileImage()));
    }

    @Override
    public RestResponse deleteQaResultsProject(String projectId, String deleteReason, String reviewComments, String approvalStatus) {
        try {
            String user = sUserService.getSUser(PlatformSessionContext.getUserID());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);

            DzqhQpBasicMapEntity qpBasicMapEntity = qpResultsRepository.queryDzqhQpBasicMap(projectId);

            DzqhJournalEntity journalEntity = new DzqhJournalEntity();

            String role = roleService.getRoleByUserId(sUser.getUserId());
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                if (!PlatformObjectUtils.isEmpty(projectId)) {
                    if (sUser.getUserId().equals(qpBasicMapEntity.getCreateUser()) && qpBasicMapEntity.getStatus().equals("0")) {
                        if (roleCode.equals(FxfzConstants.DZQH_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_GAME_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                            //管理员删除自己的数据
                            qpResultsRepository.tombstoneApAsicMap(projectId, deleteReason);
                            //记录操作痕迹
                            journalEntity.setProjectId(projectId);
                            journalEntity.setOperation("删除");
                            journalEntity.setStatus("1");
                            journalEntity.setCompany(sUser.getOrgName());
                            journalEntity.setUserName(sUser.getUserName());
                            saResultsService.saveJournal(journalEntity, sUser.getUserId());
                        }
                    } else if (!PlatformObjectUtils.isEmpty(deleteReason) && qpBasicMapEntity.getApprovalStatus().equals("0")) {
                        if (roleCode.equals(FxfzConstants.DZQH_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_GAME_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                            //管理员删除用户数据
                            String status = "3";
                            qpResultsRepository.updateDzqhQpAsicMapStatus(projectId, deleteReason, status);
                        }
                    } else if (PlatformObjectUtils.isNotEmpty(approvalStatus) && approvalStatus.equals("2") && !PlatformObjectUtils.isEmpty(reviewComments)) {
                        if (roleCode.equals(FxfzConstants.DZQH_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_GAME_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                            //管理员审批用户删除数据
                            qpResultsRepository.approvalUserDelete(projectId, reviewComments, approvalStatus, "4", "1", sUser.getUserName(), sUser.getOrgName(), PlatformDateUtils.getCurrentTimestamp());
                            saResultsRepository.updateJournalStatus(projectId, "1");
                        }
                    } else if (PlatformObjectUtils.isNotEmpty(approvalStatus) && approvalStatus.equals("3") && !PlatformObjectUtils.isEmpty(reviewComments)) {
                        if (roleCode.equals(FxfzConstants.DZQH_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_GAME_GL_ROLE) || roleCode.equals(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                            //审批不通过
                            qpResultsRepository.approvalUserDelete(projectId, reviewComments, approvalStatus, "0", "0", sUser.getUserName(), sUser.getOrgName(), PlatformDateUtils.getCurrentTimestamp());
                            saResultsRepository.updateJournalStatus(projectId, "2");
                        }
                    } else {
                        if (!qpBasicMapEntity.getCreateUser().equals(sUser.getUserId()))
                            return RestResponse.fail("删除失败，只能删除本人数据");
                        qpResultsRepository.deleteApAsicMapById(projectId, deleteReason);
                        //记录操作痕迹
                        journalEntity.setProjectId(projectId);
                        journalEntity.setOperation("删除");
                        journalEntity.setStatus("0");
                        journalEntity.setCompany(sUser.getOrgName());
                        journalEntity.setUserName(sUser.getUserName());
                        saResultsService.saveJournal(journalEntity, sUser.getUserId());
                    }
                }
            }
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
        return RestResponse.succeed("删除成功");
    }

    @Override
    public RestResponse importExcelQpResults(MultipartFile file) {
        SaResultsVo saResultsVo = new SaResultsVo();
        try {
            Workbook workBook = ExcelUtil.getWorkBook(file);
            ImportParams params = new ImportParams();
            for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) {
                params.setStartSheetIndex(numSheet);
                if (numSheet == 0) {
                    //历史地震目录
                    params.setTitleRows(1);
                    ExcelImportResult<DzqhApCruxHecEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhApCruxHecEntity.class, params);
                    List<DzqhApCruxHecEntity> dzqhApCruxHecList = result.getList();
                    saResultsVo.setDzqhApCruxHecEntityList(dzqhApCruxHecList);
                } else if (numSheet == 1) {
                    //土层，成孔信息
                    params.setTitleRows(2);
                    ExcelImportResult<DzqhApCruxLayerPfEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhApCruxLayerPfEntity.class, params);
                    List<DzqhApCruxLayerPfEntity> dzqhApCruxLayerPfList = result.getList();
                    saResultsVo.setDzqhApCruxLayerPfEntityList(dzqhApCruxLayerPfList);
                } else if (numSheet == 2) {
                    //土层，土动力参数
                    params.setTitleRows(2);
                    ExcelImportResult<DzqhApCruxSdEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhApCruxSdEntity.class, params);
                    List<DzqhApCruxSdEntity> dzqhApCruxSdList = result.getList();
                    saResultsVo.setDzqhApCruxSdEntityList(dzqhApCruxSdList);
                } else if (numSheet == 3) {
                    //土层，钻孔计算模型
                    params.setTitleRows(2);
                    ExcelImportResult<DzqhApCruxDhEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhApCruxDhEntity.class, params);
                    List<DzqhApCruxDhEntity> dzqhApCruxDhList = result.getList();
                    saResultsVo.setDzqhApCruxDhEntityList(dzqhApCruxDhList);
                } else if (numSheet == 4) {
                    //新开展的勘探测线  测线信息表格
                    params.setTitleRows(2);
                    ExcelImportResult<DzqhApCruxElEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhApCruxElEntity.class, params);
                    List<DzqhApCruxElEntity> dzqhApCruxElList = result.getList();
                    dzqhApCruxElList.forEach(it -> {
                        String testAr = it.getTestAr();
                        Map<String, String> map = SaResultsServiceImpl.analysisAddress(testAr);
                        String idByCondition = null;
                        try {
                            idByCondition = sysAreaService.getIdByCondition(URLEncoder.encode(map.get("province"), "UTF-8"), URLEncoder.encode(map.get("city"), "UTF-8"), URLEncoder.encode(map.get("county"), "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            log.error("地址转换失败:{}", e);
                        }
                        String replace = idByCondition.replace("_", ",");
                        it.setTestAr(replace);
                    });
                    saResultsVo.setDzqhApCruxElEntityList(dzqhApCruxElList);

                } else if (numSheet == 5) {
                    //场地条件评价   场地类别判别信息表
                    params.setTitleRows(2);
                    ExcelImportResult<DzqhApCruxScEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhApCruxScEntity.class, params);
                    List<DzqhApCruxScEntity> dzqhApCruxScList = result.getList();
                    dzqhApCruxScList.forEach(it -> {
                        switch (it.getFieldType()) {
                            case "Ⅰ1":
                                it.setFieldType("1");
                                break;
                            case "Ⅱ":
                                it.setFieldType("2");
                                break;
                            case "Ⅲ":
                                it.setFieldType("3");
                                break;
                            case "Ⅳ":
                                it.setFieldType("4");
                                break;
                            case "Ⅰ0":
                                it.setFieldType("10");
                                break;
                        }
                    });
                    saResultsVo.setDzqhApCruxScEntityList(dzqhApCruxScList);

                } else if (numSheet == 6) {
                    //场地地震动参数
                    params.setTitleRows(1);
                    ExcelImportResult<DzqhApCruxSgmEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhApCruxSgmEntity.class, params);
                    List<DzqhApCruxSgmEntity> dzqhApCruxSgmList = result.getList();
                    saResultsVo.setDzqhApCruxSgmEntityList(dzqhApCruxSgmList);
                } else if (numSheet == 7) {
                    params.setTitleRows(1);
                    ExcelImportResult<DzqhProfileInformationEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                            DzqhProfileInformationEntity.class, params);
                    List<DzqhProfileInformationEntity> profileInformationEntityList = result.getList();
                    if (profileInformationEntityList != null) {
                        profileInformationEntityList.forEach(it -> {
                            String testAr = it.getSectionArea();
                            Map<String, String> map = saResultsService.analysisAddress(testAr);
                            String idByCondition = null;
                            try {
                                idByCondition = sysAreaService.getIdByCondition(URLEncoder.encode(map.get("province"), "utf-8"), URLEncoder.encode(map.get("county"), "utf-8"), URLEncoder.encode(map.get("city"), "utf-8"));
                            } catch (UnsupportedEncodingException e) {
                                log.error("地址转换失败:{}", e);
                            }
                            String replace = idByCondition.replace("_", ",");
                            it.setSectionArea(replace);
                        });
                        saResultsVo.setProfileInformationList(profileInformationEntityList);
                    }
                }
            }
        } catch (Exception e) {
            log.error("导入失败:{}", e);
            return RestResponse.fail("导入失败！请检查导入文档的格式是否正确");
        }
        return RestResponse.succeed(saResultsVo);
    }

    @Override
    public void exportExcelQpResults(HttpServletResponse response, String projectId) {
        try {
            //查询需要导出的数据
            List<DzqhApCruxHecEntity> dzqhApCruxHecEntityList = saResultsRepository.queryDzqhApCruxHec(projectId);
            List<DzqhApCruxLayerPfEntity> dzqhApCruxLayerPfEntityList = saResultsRepository.queryDzqhApCruxLayerPf(projectId);
            List<DzqhApCruxSdEntity> dzqhApCruxSdEntityList = saResultsRepository.queryDzqhApCruxSd(projectId);
            List<DzqhApCruxDhEntity> dzqhApCruxDhEntityList = saResultsRepository.queryDzqhApCruxDh(projectId);
            List<DzqhApCruxScEntity> dzqhApCruxScEntityList = saResultsRepository.queryDzqhApCruxSc(projectId);
            List<DzqhApCruxElEntity> dzqhApCruxElEntityList = saResultsRepository.queryDzqhApCruxEl(projectId);
            List<DzqhApCruxSgmEntity> dzqhApCruxSgmEntityList = saResultsRepository.queryDzqhApCruxSgm(projectId);
            List<DzqhProfileInformationEntity> profileInformationList = saResultsRepository.queryprofileInfo(projectId);

            HSSFWorkbook wb = new HSSFWorkbook();

            // 样式设置
            HSSFCellStyle style = wb.createCellStyle();
            style.setFillPattern(FillPatternType.NO_FILL); // 背景
            style.setFillForegroundColor(IndexedColors.WHITE.getIndex()); // 背景色AQUA.getIndex()
            style.setBorderBottom(BorderStyle.THIN); // 下边框
            style.setBorderLeft(BorderStyle.THIN);// 左边框
            style.setBorderTop(BorderStyle.THIN);// 上边框
            style.setBorderRight(BorderStyle.THIN);// 右边框
            style.setAlignment(HorizontalAlignment.CENTER);//左右居中
            style.setVerticalAlignment(VerticalAlignment.BOTTOM);// 上下居中
            style.setWrapText(true);//自动换行

            //创建sheet1
            HSSFSheet sheet1 = wb.createSheet("历史地震目录-导出");
            HSSFRow row1 = sheet1.createRow(0);
            String[] sheet1Title = {"编号", "发震时间-日期", "发震时间-时间", "震中位置-北纬", "震中位置-东经", "震级 M", "深度 km", "震中烈度", "精度", "震中参考地名"};
            for (int i = 0; i < sheet1Title.length; i++) {
                Cell cell = row1.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet1Title[i]);
                sheet1.setColumnWidth(i, 10000);   //设置列宽
            }

            //创建sheet2
            HSSFSheet sheet2 = wb.createSheet("土层（成孔信息）-导出");
            HSSFRow row2 = sheet2.createRow(0);
            String[] sheet2Title = {"序号", "钻孔依托的项目", "钻孔编号", "孔位经度（°）", "孔位纬度（°）", "钻孔深度（m）", "地面高程（m）", "地下水位（m）"};
            for (int i = 0; i < sheet2Title.length; i++) {
                Cell cell = row2.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet2Title[i]);
                cell.setCellValue(sheet2Title[i]);
                cell.setCellValue(sheet2Title[i]);
                sheet2.setColumnWidth(i, 10000);   //设置列宽
            }

            //创建sheet3
            HSSFSheet sheet3 = wb.createSheet("土层（土动力参数）-导出");
            HSSFRow row3 = sheet3.createRow(0);
            String[] sheet3Title = {"序号", "土类号", "岩土性名称", "钻孔编号", "土样埋深", "参数来源", "密度(kg/m3)", "模量比(G/Gmax)剪应变(γ)（10^-4×0.05)", "模量比(G/Gmax)剪应变(γ)（10^-4×0.10)", "模量比(G/Gmax)剪应变(γ)（10^-4×0.50)", "模量比(G/Gmax)剪应变(γ)（10^-4×1.00)", "模量比(G/Gmax)剪应变(γ)（10^-4×5.00)",
                    "模量比(G/Gmax)剪应变(γ)（10^-4×10.00)", "模量比(G/Gmax)剪应变(γ)（10^-4×50.00)", "模量比(G/Gmax)剪应变(γ)（10^-4×100.00)", "阻尼比(λ)剪应变(γ)（10^-4×0.05)", "阻尼比(λ)剪应变(γ)（10^-4×0.10)", "阻尼比(λ)剪应变(γ)（10^-4×0.50)", "阻尼比(λ)剪应变(γ)（10^-4×1.00)", "阻尼比(λ)剪应变(γ)（10^-4×5.00)", "阻尼比(λ)剪应变(γ)（10^-4×10.00)",
                    "阻尼比(λ)剪应变(γ)（10^-4×50.00)", "阻尼比(λ)剪应变(γ)（10^-4×100.00)"};
            for (int i = 0; i < sheet3Title.length; i++) {
                Cell cell = row3.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet3Title[i]);
                sheet3.setColumnWidth(i, 10000);   //设置列宽
            }

            //创建sheet4
            HSSFSheet sheet4 = wb.createSheet("土层（钻孔计算模型）-导出");
            HSSFRow row4 = sheet4.createRow(0);
            String[] sheet4Title = {"序号", "岩土性名称", "土类号", "层底深度 (m)", "密度(kg/m^3)", "层厚(m)", "剪切波波速V(m/s)"};
            for (int i = 0; i < sheet4Title.length; i++) {
                Cell cell = row4.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet4Title[i]);
                sheet4.setColumnWidth(i, 10000);   //设置列宽
            }

            //创建sheet5
            HSSFSheet sheet5 = wb.createSheet("新开展的地球物理勘探测线-导出");
            HSSFRow row5 = sheet5.createRow(0);
            String[] sheet5Title = {"序号", "测线编号", "测线所在行政区（乡镇级别）", "起点（保留6位小数）-经度", "起点（保留6位小数）-纬度", "终点（保留6位小数）-经度", "终点（保留6位小数）-纬度", "勘探方式", "勘测基本参数（200字以内）", "长度km"};
            for (int i = 0; i < sheet5Title.length; i++) {
                Cell cell = row5.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet5Title[i]);
                sheet5.setColumnWidth(i, 10000);   //设置列宽
            }

            //创建sheet6
            HSSFSheet sheet6 = wb.createSheet("场地条件评价-导出");
            HSSFRow row6 = sheet6.createRow(0);
            String[] sheet6Title = {"序号", "钻孔编号（与土层模型相对应）", "20m等效剪切波速（M/s）", "覆盖层厚度m", "场地类别"};
            for (int i = 0; i < sheet6Title.length; i++) {
                Cell cell = row6.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet6Title[i]);
                sheet6.setColumnWidth(i, 10000);   //设置列宽
            }

            //创建sheet7
            HSSFSheet sheet7 = wb.createSheet("场地地震动参数-导出");
            HSSFRow row7 = sheet7.createRow(0);
            String[] sheet7Title = {"序号", "超越概率", "Amax (gal)", "βm", "αmax", "T1 (s)", "Tg(s)", "γ", "阻尼比"};
            for (int i = 0; i < sheet7Title.length; i++) {
                Cell cell = row7.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet7Title[i]);
                sheet7.setColumnWidth(i, 10000);   //设置列宽
            }

            HSSFSheet sheet8 = wb.createSheet("地质剖面信息-导出");
            HSSFRow row8 = sheet8.createRow(0);
            String[] sheet8Title = {"序号", "剖面名称", "剖面所在行政区（乡镇级别）", "经度", "纬度"};
            for (int i = 0; i < sheet8Title.length; i++) {
                Cell cell = row8.createCell(i);
                cell.setCellStyle(style);     //设置标题样式
                cell.setCellValue(sheet8Title[i]);
                sheet8.setColumnWidth(i, 10000);   //设置列宽
            }


            if (null != dzqhApCruxHecEntityList && dzqhApCruxHecEntityList.size() > 0) {
                for (int i = 0; i < dzqhApCruxHecEntityList.size(); i++) {
                    DzqhApCruxHecEntity dzqhApCruxHec = dzqhApCruxHecEntityList.get(i);
                    //设置 sheet1 中的值
                    row1 = sheet1.createRow(sheet1.getLastRowNum() + 1);
                    row1.createCell(0).setCellValue(i + 1);
                    row1.createCell(1).setCellValue(dzqhApCruxHec.getEoTimeYmd());
                    SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");
                    String hmsTime = "";
                    if (dzqhApCruxHec.getEoTimeHms() != null) {
                        try {
                            if (StrUtil.isNotBlank(dzqhApCruxHec.getEoTimeHms())) {
                                Date date = hms.parse(dzqhApCruxHec.getEoTimeHms());
                                hmsTime = hms.format(date);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    row1.createCell(2).setCellValue(hmsTime);
                    row1.createCell(3).setCellValue(dzqhApCruxHec.getNorthLatitude());
                    row1.createCell(4).setCellValue(dzqhApCruxHec.getEastLongitude());
                    row1.createCell(5).setCellValue(dzqhApCruxHec.getMagnitude());
                    row1.createCell(6).setCellValue(dzqhApCruxHec.getDepth());
                    row1.createCell(7).setCellValue(dzqhApCruxHec.getEpicentralIntensity());
                    row1.createCell(8).setCellValue(dzqhApCruxHec.getAccuracy());
                    row1.createCell(9).setCellValue(dzqhApCruxHec.getErpName());
                }
            }
            //设置 sheet2 中的值
            if (null != dzqhApCruxLayerPfEntityList && dzqhApCruxLayerPfEntityList.size() > 0) {
                for (int i = 0; i < dzqhApCruxLayerPfEntityList.size(); i++) {
                    DzqhApCruxLayerPfEntity dzqhApCruxLayerPfEntity = dzqhApCruxLayerPfEntityList.get(i);
                    row2 = sheet2.createRow(sheet2.getLastRowNum() + 1);
                    row2.createCell(0).setCellValue(i + 1);
                    row2.createCell(1).setCellValue(dzqhApCruxLayerPfEntity.getProjectName());
                    row2.createCell(2).setCellValue(dzqhApCruxLayerPfEntity.getBoreholeNumber());
                    row2.createCell(3).setCellValue(dzqhApCruxLayerPfEntity.getHlLongitude());
                    row2.createCell(4).setCellValue(dzqhApCruxLayerPfEntity.getHlLatitude());
                    row2.createCell(5).setCellValue(dzqhApCruxLayerPfEntity.getDrillingDepth());
                    row2.createCell(6).setCellValue(dzqhApCruxLayerPfEntity.getGroundElevation());
                    row2.createCell(7).setCellValue(dzqhApCruxLayerPfEntity.getGroundwaterLevel());
                }
            }

            //设置 sheet3 中的值
            if (null != dzqhApCruxSdEntityList && dzqhApCruxSdEntityList.size() > 0) {
                for (int i = 0; i < dzqhApCruxSdEntityList.size(); i++) {
                    DzqhApCruxSdEntity dzqhApCruxSdEntity = dzqhApCruxSdEntityList.get(i);
                    row3 = sheet3.createRow(sheet3.getLastRowNum() + 1);
                    row3.createCell(0).setCellValue(i + 1);
                    row3.createCell(1).setCellValue(dzqhApCruxSdEntity.getStNumber());
                    row3.createCell(2).setCellValue(dzqhApCruxSdEntity.getGeotechnicalName());
                    row3.createCell(3).setCellValue(dzqhApCruxSdEntity.getBoreholeNumber());
                    row3.createCell(4).setCellValue(dzqhApCruxSdEntity.getBdosSample());
                    row3.createCell(5).setCellValue(dzqhApCruxSdEntity.getParameterSource());
                    row3.createCell(6).setCellValue(dzqhApCruxSdEntity.getSdDensity());
                    if (dzqhApCruxSdEntity.getModulusRatio1() != null) {
                        row3.createCell(7).setCellValue(dzqhApCruxSdEntity.getModulusRatio1());
                    }
                    if (dzqhApCruxSdEntity.getModulusRatio2() != null) {
                        row3.createCell(8).setCellValue(dzqhApCruxSdEntity.getModulusRatio2());
                    }
                    if (dzqhApCruxSdEntity.getModulusRatio3() != null) {
                        row3.createCell(9).setCellValue(dzqhApCruxSdEntity.getModulusRatio3());
                    }
                    if (dzqhApCruxSdEntity.getModulusRatio4() != null) {
                        row3.createCell(10).setCellValue(dzqhApCruxSdEntity.getModulusRatio4());
                    }
                    if (dzqhApCruxSdEntity.getModulusRatio5() != null) {
                        row3.createCell(11).setCellValue(dzqhApCruxSdEntity.getModulusRatio5());
                    }
                    if (dzqhApCruxSdEntity.getModulusRatio6() != null) {
                        row3.createCell(12).setCellValue(dzqhApCruxSdEntity.getModulusRatio6());
                    }
                    if (dzqhApCruxSdEntity.getModulusRatio7() != null) {
                        row3.createCell(13).setCellValue(dzqhApCruxSdEntity.getModulusRatio7());
                    }
                    if (dzqhApCruxSdEntity.getModulusRatio8() != null) {
                        row3.createCell(14).setCellValue(dzqhApCruxSdEntity.getModulusRatio8());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio1() != null) {
                        row3.createCell(15).setCellValue(dzqhApCruxSdEntity.getDampingRatio1());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio2() != null) {
                        row3.createCell(16).setCellValue(dzqhApCruxSdEntity.getDampingRatio2());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio3() != null) {
                        row3.createCell(17).setCellValue(dzqhApCruxSdEntity.getDampingRatio3());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio4() != null) {
                        row3.createCell(18).setCellValue(dzqhApCruxSdEntity.getDampingRatio4());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio5() != null) {
                        row3.createCell(19).setCellValue(dzqhApCruxSdEntity.getDampingRatio5());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio6() != null) {
                        row3.createCell(20).setCellValue(dzqhApCruxSdEntity.getDampingRatio6());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio7() != null) {
                        row3.createCell(21).setCellValue(dzqhApCruxSdEntity.getDampingRatio7());
                    }
                    if (dzqhApCruxSdEntity.getDampingRatio8() != null) {
                        row3.createCell(22).setCellValue(dzqhApCruxSdEntity.getDampingRatio8());
                    }
                }
            }

            //设置 sheet4 中的值
            if (null != dzqhApCruxDhEntityList && dzqhApCruxDhEntityList.size() > 0) {
                for (int i = 0; i < dzqhApCruxDhEntityList.size(); i++) {
                    DzqhApCruxDhEntity dzqhApCruxDhEntity = dzqhApCruxDhEntityList.get(i);
                    row4 = sheet4.createRow(sheet4.getLastRowNum() + 1);
                    row4.createCell(0).setCellValue(i + 1);
                    row4.createCell(1).setCellValue(dzqhApCruxDhEntity.getGeotechnicalName());
                    row4.createCell(2).setCellValue(dzqhApCruxDhEntity.getStNumber());
                    row4.createCell(3).setCellValue(dzqhApCruxDhEntity.getLbDepth());
                    row4.createCell(4).setCellValue(dzqhApCruxDhEntity.getGeotechnicalDensity());
                    row4.createCell(5).setCellValue(dzqhApCruxDhEntity.getLayerThickness());
                    row4.createCell(6).setCellValue(dzqhApCruxDhEntity.getSwVelocity());
                }
            }

            //设置 sheet5 中的值
            if (null != dzqhApCruxElEntityList && dzqhApCruxElEntityList.size() > 0) {
                for (int i = 0; i < dzqhApCruxElEntityList.size(); i++) {
                    DzqhApCruxElEntity dzqhApCruxElEntity = dzqhApCruxElEntityList.get(i);
                    row5 = sheet5.createRow(sheet5.getLastRowNum() + 1);
                    row5.createCell(0).setCellValue(i + 1);
                    row5.createCell(1).setCellValue(dzqhApCruxElEntity.getTestNumber());
                    row6.createCell(2).setCellValue(sysAreaService.getAreaNameByAreaId(dzqhApCruxElEntity.getTestAr()));
                    row5.createCell(3).setCellValue(dzqhApCruxElEntity.getStartLongitude());
                    row5.createCell(4).setCellValue(dzqhApCruxElEntity.getStartLatitude());
                    row5.createCell(5).setCellValue(dzqhApCruxElEntity.getStartLatitude());
                    row5.createCell(6).setCellValue(dzqhApCruxElEntity.getEndLongitude());
                    row5.createCell(7).setCellValue(dzqhApCruxElEntity.getEndLatitude());
                    row5.createCell(8).setCellValue(dzqhApCruxElEntity.getSurveyMethod());
                    row5.createCell(9).setCellValue(dzqhApCruxElEntity.getSurveyParameters());
                    row5.createCell(10).setCellValue(dzqhApCruxElEntity.getTestLength());
                }
            }

            //设置 sheet6 中的值
            if (null != dzqhApCruxScEntityList && dzqhApCruxScEntityList.size() > 0) {
                for (int i = 0; i < dzqhApCruxScEntityList.size(); i++) {
                    DzqhApCruxScEntity dzqhApCruxScEntity = dzqhApCruxScEntityList.get(i);
                    row6 = sheet6.createRow(sheet6.getLastRowNum() + 1);
                    row6.createCell(0).setCellValue(i + 1);
                    row6.createCell(1).setCellValue(dzqhApCruxScEntity.getBoreholeNumber());
                    row6.createCell(2).setCellValue(dzqhApCruxScEntity.getEswv());
                    row6.createCell(3).setCellValue(dzqhApCruxScEntity.getTocLayer());
                    switch (dzqhApCruxScEntity.getFieldType()) {
                        case "1":
                            row6.createCell(4).setCellValue("Ⅰ1");
                            break;
                        case "2":
                            row6.createCell(4).setCellValue("Ⅱ");
                            break;
                        case "3":
                            row6.createCell(4).setCellValue("Ⅲ");
                            break;
                        case "4":
                            row6.createCell(4).setCellValue("Ⅳ");
                            break;
                        case "10":
                            row6.createCell(4).setCellValue("Ⅰ0");
                            break;
                    }
                }
            }

            //设置 sheet7 中的值
            if (null != dzqhApCruxSgmEntityList && dzqhApCruxSgmEntityList.size() > 0) {
                for (int i = 0; i < dzqhApCruxSgmEntityList.size(); i++) {
                    DzqhApCruxSgmEntity dzqhApCruxSgmEntity = dzqhApCruxSgmEntityList.get(i);
                    row7 = sheet7.createRow(sheet7.getLastRowNum() + 1);
                    row7.createCell(0).setCellValue(i + 1);
                    row7.createCell(1).setCellValue(dzqhApCruxSgmEntity.getTranscendProbability());
                    row7.createCell(2).setCellValue(dzqhApCruxSgmEntity.getAmaxGal());
                    row7.createCell(3).setCellValue(dzqhApCruxSgmEntity.getBm());
                    row7.createCell(4).setCellValue(dzqhApCruxSgmEntity.getAmax());
                    row7.createCell(5).setCellValue(dzqhApCruxSgmEntity.getT1S());
                    row7.createCell(6).setCellValue(dzqhApCruxSgmEntity.getTgS());
                    row7.createCell(7).setCellValue(dzqhApCruxSgmEntity.getV());
                    row7.createCell(8).setCellValue(dzqhApCruxSgmEntity.getDampingRatio());
                }
            }

            if (profileInformationList != null && profileInformationList.size() > 0) {
                for (int i = 0; i < profileInformationList.size(); i++) {
                    DzqhProfileInformationEntity profileInformationEntity = profileInformationList.get(i);
                    row8 = sheet8.createRow(sheet8.getLastRowNum() + 1);
                    row8.createCell(0).setCellValue(i + 1);
                    row8.createCell(1).setCellValue(profileInformationEntity.getBisectName());
                    row8.createCell(2).setCellValue(profileInformationEntity.getSectionArea());
                    row8.createCell(3).setCellValue(profileInformationEntity.getLongitude());
                    row8.createCell(4).setCellValue(profileInformationEntity.getLatitude());
                }
            }

            String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            String name = dateStr + "区域性数据.xls";
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Connection", "close"); // 表示不能用浏览器直接打开
            response.setHeader("Content-Disposition", "attachment;fileName="
                    + new String(name.getBytes("GB2312"), "ISO8859-1"));
            response.setCharacterEncoding("UTF-8");

            OutputStream out = response.getOutputStream();
            wb.write(out);
            if (out != null) {
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public RestResponse notSubmitted() {
        try {
            HashMap<String, Object> map = new HashMap<>();
            String userId = PlatformSessionContext.getUserID();
            //根据当前登录的用户id查询出未提交数据
            List<DzqhQpBasicMapEntity> qpBasicMapEntityList = qpResultsRepository.queryNotSubmitted(userId);
            SaResultsVo saResultsVo = new SaResultsVo();
            if (qpBasicMapEntityList != null) {
                for (DzqhQpBasicMapEntity qpBasicMapEntity : qpBasicMapEntityList) {
                    queryFile(map, qpBasicMapEntity.getProjectId());
                    saResultsVo.setDzqhQpBasicMapEntity(qpBasicMapEntity);
                    saResultsVo.setDzqhApCruxDhEntityList(saResultsRepository.queryDzqhApCruxDh(qpBasicMapEntity.getProjectId()));
                    List<DzqhApCruxElEntity> dzqhApCruxElEntities = saResultsRepository.queryDzqhApCruxEl(qpBasicMapEntity.getProjectId());
                    dzqhApCruxElEntities.forEach(it -> {
                        if (PlatformObjectUtils.isNotEmpty(it.getPposlMap())) {
                            AttachmentInfoEntity page = attachmentInfoRepository.getAttach(it.getPposlMap());
                            it.setPposlMapPage(page);
                        }
                    });
                    saResultsVo.setDzqhApCruxElEntityList(dzqhApCruxElEntities);
                    saResultsVo.setDzqhApCruxHecEntityList(saResultsRepository.queryDzqhApCruxHec(qpBasicMapEntity.getProjectId()));
                    saResultsVo.setDzqhApCruxLayerPfEntityList(saResultsRepository.queryDzqhApCruxLayerPf(qpBasicMapEntity.getProjectId()));
                    saResultsVo.setDzqhApCruxScEntityList(saResultsRepository.queryDzqhApCruxSc(qpBasicMapEntity.getProjectId()));
                    saResultsVo.setDzqhApCruxSdEntityList(saResultsRepository.queryDzqhApCruxSd(qpBasicMapEntity.getProjectId()));
                    saResultsVo.setDzqhApCruxSgmEntityList(saResultsRepository.queryDzqhApCruxSgm(qpBasicMapEntity.getProjectId()));
                    List<DzqhProfileInformationEntity> list = saResultsRepository.queryProfileInformationList(qpBasicMapEntity.getProjectId());
                    list.forEach(it -> {
                        if (PlatformObjectUtils.isNotEmpty(it.getPposlMap())) {
                            AttachmentInfoEntity page = attachmentInfoRepository.getAttach(it.getPposlMap());
                            it.setPposlMapPage(page);
                        }
                    });
                    saResultsVo.setProfileInformationList(list);
                    saResultsVo.setFileMap(map);
                }
            }
            return RestResponse.succeed(saResultsVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse queryAllQpProject() {
        try {
            List<DzqhQpBasicMapEntity> dzqhQpBasicMapList = qpResultsRepository.queryAllQpProject();
            return RestResponse.succeed(dzqhQpBasicMapList);
        } catch (Exception e) {
            String errorMessage = ("查询失败");
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getQpResultsProject(DzqhQpBasicMapEntity qpBasicMapEntity, HttpServletRequest request, String geom) {
        try {
            int page = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String userId = PlatformSessionContext.getUserID();
            String role = roleService.getRoleByUserId(userId);
            String user = sUserService.getSUser(PlatformSessionContext.getUserID());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            //单位id
            String orgId = sUser.getOrgId();
            //省级id
            String provinceId = sUser.getProvinceId();
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            ArrayList<String> roleList = new ArrayList<>();
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                roleList.add(roleCode);
            }
            if (roleList.contains(FxfzConstants.DZQH_GL_ROLE)) {
                //系统管理员
                qpBasicMapEntity.setRole("0");
            } else if (roleList.contains(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                //省级管理员
                qpBasicMapEntity.setRole("1");
            } else if (roleList.contains(FxfzConstants.DZQH_GAME_GL_ROLE)) {
                //局级管理员
                qpBasicMapEntity.setRole("2");
            } else {
                //普通用户
                qpBasicMapEntity.setRole("3");
            }
            HashMap<String, Object> map = qpResultsRepository.getQpResultsProject(qpBasicMapEntity, page, size, orgId, provinceId, userId, geom);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            String errorMessage = "区评成果目录项目信息查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getQpResultsHl(SaResultsHlVo saResultsHlVo, HttpServletRequest request, String geom) {
        try {
            int page = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String userId = PlatformSessionContext.getUserID();
            String role = roleService.getRoleByUserId(userId);
            String user = sUserService.getSUser(PlatformSessionContext.getUserID());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            //单位id
            String orgId = sUser.getOrgId();
            //省级id
            String provinceId = sUser.getProvinceId();
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                if (roleCode.equals(FxfzConstants.DZQH_GL_ROLE)) {
                    //系统管理员
                    saResultsHlVo.setRole("0");
                } else if (roleCode.equals(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                    //省级管理员
                    saResultsHlVo.setRole("1");
                } else if (roleCode.equals(FxfzConstants.DZQH_GAME_GL_ROLE)) {
                    //局级管理员
                    saResultsHlVo.setRole("2");
                } else {
                    //普通用户
                    saResultsHlVo.setRole("3");
                }
            }
            return RestResponse.succeed(qpResultsRepository.getSaResultsHl(saResultsHlVo, page, size, orgId, provinceId, userId, geom));
        } catch (Exception e) {
            String errorMessage = "安评成果目录孔位信息查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(ApTemporarySaveParam param) {
        AddQpParam qpParam = param.getAddQpParam();
        String projectId = UUIDGenerator.getUUID();
        String logId = UUIDGenerator.getUUID();
        String userId = PlatformSessionContext.getUserID();
        String sUser = sUserService.getSUser(userId);
        SUser user = CreateGson.createGson().fromJson(sUser, SUser.class);
        ArrayList<DzqhJournalEntity> logList = new ArrayList<>();
        DzqhQpBasicMapEntity qpEntity = qpResultsRepository.queryDzqhQpBasicMap(qpParam.getProjectId());
        if (PlatformObjectUtils.isNotEmpty(qpEntity)) {
            if (qpParam.getProjectId().equals(qpEntity.getProjectId()) && qpEntity.getSubStatus().equals("0")) {
                DzqhQpBasicMapEntity entity = new DzqhQpBasicMapEntity();
                projectId = qpParam.getProjectId();
                BeanUtils.copyProperties(qpParam, entity);
                param.setType("0");
                qpResultsRepository.updateDzqhQpBasicMap(entity);
                saResultsService.dataProcessing(param, logList, projectId, user, logId, FxfzConstants.DZQH_QP_MODEL);
                return;
            }
        }
        //新增操作，如果是提交，走保存逻辑，原始数据id为projectId    如果是保存，走修改
        if (PlatformObjectUtils.isNotEmpty(qpParam.getProjectId()) && qpParam.getSubStatus().equals("0")) {
            DzqhQpBasicMapEntity entity = new DzqhQpBasicMapEntity();
            projectId = qpParam.getProjectId();
            param.setType("0");
            BeanUtils.copyProperties(qpParam, entity);
            qpResultsRepository.updateDzqhQpBasicMap(entity);
        } else {
            //新增
            DzqhQpBasicMapEntity entity = new DzqhQpBasicMapEntity();
            BeanUtils.copyProperties(qpParam, entity);
            entity.setProjectId(projectId);
            entity.setIsDelete("0");
            entity.setStatus("0");
            entity.setApprovalStatus("0");
            entity.setLogId(logId);
            entity.setOriginalDataId(projectId);
            entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setOrgId(user.getOrgId());
            entity.setUpdateStatus("0");
            entity.setProvinceId(user.getProvinceId());
            entity.setModel(FxfzConstants.DZQH_QP_MODEL);
            entity.setRegistrant(user.getUserName());
            param.setType("0");
            if (qpParam.getSubStatus().equals("1") && PlatformObjectUtils.isNotEmpty(qpParam.getProjectId())) {
                entity.setCreateUser(qpEntity.getCreateUser());
                entity.setCreateTime(qpEntity.getCreateTime());
                entity.setUpdateUser(PlatformSessionContext.getUserID());
                entity.setUpdateStatus("1");
                entity.setLogId(qpEntity.getLogId());
                logId = qpEntity.getLogId();
                entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                entity.setOriginalDataId(qpEntity.getProjectId());
                AddQpParam oldParam = new AddQpParam();
                param.setType("1");
                BeanUtils.copyProperties(qpEntity, oldParam);
                logList.addAll(recordUtils.addChangeRecord(oldParam, qpParam, logId, "项目基本情况表", user.getUserName(), user.getOrgName(), userId));
            }
            if (PlatformObjectUtils.isNotEmpty(qpParam.getUpdateReason())) {
                entity.setUpdateReason(qpParam.getUpdateReason());
                entity.setStatus("2");
                entity.setApprovalStatus("1");
                qpResultsRepository.updateStatus(entity, qpEntity.getProjectId());
            }
            qpResultsRepository.saveDzqhQpBasicMap(entity);
        }

        //保存日志记录
        param.setSubStatus(qpParam.getSubStatus());
        ArrayList<DzqhJournalEntity> list = saResultsService.dataProcessing(param, logList, projectId, user, logId, FxfzConstants.DZQH_QP_MODEL);
        list.forEach(it -> {
            saResultsRepository.saveJournal(it);
        });
    }
}
